package com.smartstate.iot.tool;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartstate.iot.entity.TopicConfig;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.service.TopicLogService;
import com.smartstate.iot.service.TopicService;
import com.smartstate.iot.vo.TopicUpVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
@Slf4j
public class TopicTool {

    @Autowired
    private HandleService handleService;
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicLogService topicLogService;

    /**
     * 上条时间
     */
    private Long er = 0L;

    /**
     * topic判断结果为盒子上行心跳数据  存储到数据库
     */
    public Boolean saveTopicConfig(TopicUpVo topicUpVo) {
        for (TopicUpVo.Content content : topicUpVo.getContent()) {
            TopicConfig topicConfig = new TopicConfig();
            topicConfig.setDeviceId(content.getDeviceId());
            topicConfig.setDeviceType(content.getDeviceType());
            topicConfig.setDeviceName(content.getDeviceName());
            topicConfig.setConfig(topicUpVo.getConfig());
            topicConfig.setTimesTamps(topicUpVo.getTimesTamp());
            topicConfig.setIp(content.getIp());
            topicConfig.setCpu(content.getCpu());
            topicConfig.setPort(content.getPort());
            topicConfig.setMomery(content.getMomery());

            // 日志信息准备
            TopicLog topicLog = new TopicLog();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(new DateTime());
            topicLog.setDate(format);
            topicLog.setLogName(topicConfig.getDeviceName());
            topicLog.setDeviceId(topicConfig.getDeviceId());
            // 判断这个deviceID是否存在
            if (handleService.logOnly(topicConfig.getDeviceId())) {
                //不存在新增一条设备心跳记录
                //顺便加上一个心跳日志
                topicService.save(topicConfig);
                //判断日志表中是否已有该重复设备id  如果有就删除
                if (topicLogService.getDeviceIdOfOne(topicConfig.getDeviceId()) > 0){
                        //删除原保存日志信息
                    topicLogService.deleteOfDeviceId(topicConfig.getDeviceId());
                }
                //  判断本次信息是否全部有数据
                if (handleService.getNull(content)) {
                    //本次设备日志为正常
                    topicLog.setType("正常");
                    topicLog.setDetail("设备状态正常");
                } else {
                    topicLog.setType("异常");
                    topicLog.setDetail("设备状态异常");
                }
                topicLogService.save(topicLog);
            } else {
                // 当前设备之前时间戳
                String lastTimesTamp = topicService.getTimesTamps(topicConfig.getDeviceId());
                long l = Long.parseLong(lastTimesTamp);
//                int lastTimesTamps = Integer.parseInt(lastTimesTamp);
                //本次时间戳
                String thisTimesTamp = topicConfig.getTimesTamps();
                long l1 = Long.parseLong(thisTimesTamp);
//                int thisTimesTamps = Integer.parseInt(thisTimesTamp);
                //判断之前时间戳与本次记录时间戳相差时间判断状态变化
                //先判断当前时间与上次时间如果小于10秒 并且字段都有信息  设备为正常
                if (l1 - l <= 10000) {
                    //  判断本次信息是否全部有数据
                    if (handleService.getNull(content).equals(true)) {
                        //本次设备日志为正常
                        topicLog.setType("正常");
                        topicLog.setDetail("设备状态正常");
                    } else {
                        topicLog.setType("异常");
                        topicLog.setDetail("设备状态异常");
                    }
                }else {
                    topicLog.setType("离线");
                    topicLog.setDetail("设备状态离线");
                }
                //最后判断日志表是否已经存在此设备日志信息
                if (topicLogService.getDeviceIdOfOne(topicConfig.getDeviceId()) > 0) {
                    //日志表中已存在此设备日志表  就只进行修改日志信息
                    Integer logOk = topicLogService.updateLogByDeviceId(topicConfig.getDeviceId(), topicLog);
                    if (logOk > 0) {
                        System.out.println("日志修改成功");
                    } else {
                        System.out.println("日志修改失败");
                    }
                } else {
                    //数据库没有此设备日志信息 代表是第一次进来 就添加此设备日志信息
                    topicLogService.save(topicLog);
                }
                //存在更新这条设备心跳信息
                topicService.updateConfig(topicConfig);
            }
        }
        return true;
    }




    /**
     * 判断是否为首次
     *
     * @param topicUpVo
     * @return
     */
    public Boolean isFirstHeartbeat(TopicUpVo topicUpVo) {
        Boolean flag = false;
        List<TopicUpVo.Content> content = topicUpVo.getContent();
        for (TopicUpVo.Content content1 : content) {
            String deviceId = content1.getDeviceId();
            LambdaQueryWrapper<TopicConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TopicConfig::getDeviceId, deviceId);
            List<TopicConfig> list = topicService.list(queryWrapper);
            if (list.size() == 0) {
                flag = true;
            }
        }
        return flag;
    }


}

